package org.jadelyre.core.worker;

import org.jadelyre.core.Channel;
import org.jadelyre.core.ThreadPool;
import org.jadelyre.core.codec.Packet;
import org.jadelyre.core.task.EntryQueue;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadPoolExecutor;

public abstract class WorkerFactory {

    /**
     * 解码线程池
     */
//    private final Worker decodeWorker;
    /**
     * 业务处理线程池
     */
//    private final Worker handleWorker;
    /**
     * 发送数据包线程池
     */
//    private final Worker writeWorker;

    /**
     * 关闭连接线程池
     */
//    private final Worker closeWorker;

//    private final Worker scheduleWorker;
    public WorkerFactory() {
//        decodeWorker = new Worker();
//        handleWorker = new Worker(ThreadPool.createThreadPoolExecutor(getHandleThreadPoolEnum()));
//        writeWorker = new Worker(ThreadPool.createThreadPoolExecutor(getWriteThreadPoolEnum()));
//        closeWorker = new Worker(ThreadPool.createThreadPoolExecutor(getCloseThreadPoolEnum()));
//        scheduleWorker = new Worker(new ScheduledThreadPoolExecutor(JadelyreConfig.availableProcessors()));
    }

    public Worker<ByteBuffer> getDecodeWorker(Channel channel, EntryQueue<ByteBuffer> entryQueue) {
        ThreadPoolExecutor threadPoolExecutor = ThreadPool.createThreadPoolExecutor(getDecodeThreadPoolEnum());
        DecodeWorker decodeWorker = new DecodeWorker(channel, threadPoolExecutor, entryQueue);
        return decodeWorker;
    }

    public Worker<Packet> getHandleWorker(Channel channel, EntryQueue<Packet> entryQueue) {
        ThreadPoolExecutor threadPoolExecutor = ThreadPool.createThreadPoolExecutor(getHandleThreadPoolEnum());
        HandleWorker handleWorker = new HandleWorker(channel, threadPoolExecutor, entryQueue);
        return handleWorker;
    }

    public Worker<Packet> getWriteWorker(Channel channel, EntryQueue<Packet> entryQueue) {
        ThreadPoolExecutor threadPoolExecutor = ThreadPool.createThreadPoolExecutor(getHandleThreadPoolEnum());
        SendWorker writeWorker = new SendWorker(channel, threadPoolExecutor, entryQueue);
        return writeWorker;
    }

    public Worker<Channel> getCloseWorker(EntryQueue<Channel> entryQueue) {
        ThreadPoolExecutor threadPoolExecutor = ThreadPool.createThreadPoolExecutor(getCloseThreadPoolEnum());
        CloseWorker closeWorker = new CloseWorker(threadPoolExecutor, entryQueue);
        return closeWorker;
    }

//    public Worker getScheduleWorker(){
//        return scheduleWorker;
//    }

    //    public void shutdown(){
//        decodeWorker.shutdown();
//        handleWorker.shutdown();
//        writeWorker.shutdown();
////        encodeWorker.shutdown();
//        if (closeWorker != null){
//            closeWorker.shutdown();
//        }
//        scheduleWorker.shutdown();
//    }
    protected abstract ThreadPool.ThreadPoolEnum getDecodeThreadPoolEnum();

    //编码线程池取消，因为多余，发送数据包线程池代替
    //protected abstract ThreadPool.ThreadPoolEnum getEncodeThreadPoolEnum();
    protected abstract ThreadPool.ThreadPoolEnum getHandleThreadPoolEnum();

    protected abstract ThreadPool.ThreadPoolEnum getWriteThreadPoolEnum();

    protected abstract ThreadPool.ThreadPoolEnum getCloseThreadPoolEnum();
}
